package utils

import (
	"crypto/md5"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
	"io"
	"mime/multipart"
)

const SALT = "zhichitianya"

// Sha256加密
func Sha256(password string) string {
	hash := sha256.New()
	hash.Write([]byte(password + SALT))
	bytes := hash.Sum(nil)
	hashCode := hex.EncodeToString(bytes)
	return string(hashCode)
}

// md5加密一个文件，得到等长的hash字符串，用于比较文件是否被篡改
func Md5File(file *multipart.FileHeader) string {
	hash := md5.New()
	fileOpen, _ := file.Open()
	defer fileOpen.Close()
	if _, err := io.Copy(hash, fileOpen); err != nil {
		return ""
	}
	hashValue := hash.Sum(nil)
	return fmt.Sprintf("%x", hashValue)
}
